package com.scau.action.business;

import java.io.Serializable;
import java.sql.Date;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.ServletConfig;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.opensymphony.xwork2.ModelDriven;
import com.scau.action.BaseAction;
import com.scau.exception.BusinessException;
import com.scau.model.business.CancelOut;
import com.scau.model.business.CancelShoe;
import com.scau.model.business.OrderShoe;
import com.scau.model.business.Shoe;
import com.scau.model.business.ShoeWarehouse;
import com.scau.service.impl.business.CancelOutService;
import com.scau.service.impl.business.OrderShoeService;
import com.scau.service.impl.business.ShoeService;
import com.scau.service.impl.business.ShoeWarehouseService;
import com.scau.util.PageController;
/**
 * 此类需要修改，commented by yhl
 * @author yhl
 *
 */
@Component
@Scope("prototype")
public class CancelOutAction extends BaseAction implements
		ModelDriven<CancelOut>, Serializable {
	private static final long serialVersionUID = 8299975587235537983L;
	private final static Log logger = LogFactory.getLog(OrderListAction.class);
	private PageController pager = null;
	private CancelOut cancelOut = null;
	private CancelOutService cancelOutService = null;
	OrderShoeService orderShoeService;
	ShoeService shoeService;
	ShoeWarehouseService shoeWarehouseService;

	/**
	 * 列表显示
	 * 
	 * @return
	 */
	public String list() {
		String order_shoe_id = request.getParameter("order_shoe_id");// 订单鞋款的id
		int totalRows = cancelOutService.getRecordCount(cancelOut);
		String URL =getListURL();
		this.pager.setURL(URL);
		this.pager.setTotalRowsAmount(totalRows);
		List<CancelOut> resourceList = cancelOutService.list(new CancelOut(),
				this.pager.getPageStartRow(), pager.getPageSize(), null, null);

		List<String> product_id_list = new ArrayList<String>();// 在页面中显示相应的成品料号
		OrderShoe orderShoe = new OrderShoe();

		Shoe shoe = new Shoe();

		for (CancelOut cancelOut : resourceList) {
			orderShoe.setId(cancelOut.getOrder_shoe_id());
			shoe.setId(orderShoeService.get(orderShoe).getShoe_id());
			product_id_list.add(shoeService.get(shoe).getProduct_id());
		}
		request.setAttribute("product_id_list", product_id_list);
		this.pager.setData(resourceList);

		return "list";
	}

	/**
	 * 修改
	 * 
	 * @return
	 */
	public String get() {
		String order_shoe_id = request.getParameter("order_shoe_id");// 订单鞋款的id
		OrderShoe orderShoe = new OrderShoe();
		OrderShoeService orderShoeService = new OrderShoeService();
		orderShoe.setId(Long.parseLong(order_shoe_id));
		orderShoe = orderShoeService.get(orderShoe);

		// 以下获取仓库内已有的该成品料号半成品鞋子的数量
		Long[] owned = { 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L };
		ShoeWarehouse shoeWarehouse = new ShoeWarehouse();
		shoeWarehouse.setShoe_id(orderShoe.getShoe_id());
		shoeWarehouse = shoeWarehouseService.get(shoeWarehouse);
		if (null != shoeWarehouse) {
			owned[0] = shoeWarehouse.getSize_33();
			owned[1] = shoeWarehouse.getSize_34();
			owned[2] = shoeWarehouse.getSize_35();
			owned[3] = shoeWarehouse.getSize_36();
			owned[4] = shoeWarehouse.getSize_37();
			owned[5] = shoeWarehouse.getSize_38();
			owned[6] = shoeWarehouse.getSize_39();
			owned[7] = shoeWarehouse.getSize_40();
		}
		request.setAttribute("order_shoe_id", order_shoe_id);
		request.setAttribute("shoe_id", orderShoe.getShoe_id());
		request.setAttribute("owned", owned);
		return "edit";
	}

	/**
	 * 保存
	 * 
	 * @return
	 */
	public String save() {
		try {
			ShoeWarehouse shoeWarehouse = new ShoeWarehouse();
			ShoeWarehouseService shoeWarehouseService = new ShoeWarehouseService();
			shoeWarehouse.setShoe_id(cancelOut.getShoe_id());
			shoeWarehouse = shoeWarehouseService.get(shoeWarehouse);
			// 修改冲单中该鞋款的剩余库存
			shoeWarehouse.setSize_33(shoeWarehouse.getSize_33()
					- cancelOut.getSize_33());
			shoeWarehouse.setSize_34(shoeWarehouse.getSize_34()
					- cancelOut.getSize_34());
			shoeWarehouse.setSize_35(shoeWarehouse.getSize_35()
					- cancelOut.getSize_35());
			shoeWarehouse.setSize_36(shoeWarehouse.getSize_36()
					- cancelOut.getSize_36());
			shoeWarehouse.setSize_37(shoeWarehouse.getSize_37()
					- cancelOut.getSize_37());
			shoeWarehouse.setSize_38(shoeWarehouse.getSize_38()
					- cancelOut.getSize_38());
			shoeWarehouse.setSize_39(shoeWarehouse.getSize_39()
					- cancelOut.getSize_39());
			shoeWarehouse.setSize_40(shoeWarehouse.getSize_40()
					- cancelOut.getSize_40());
			shoeWarehouseService.save(shoeWarehouse);
			cancelOutService.save(cancelOut);
			return "list";
		} catch (BusinessException e) {

			request.setAttribute("cancelShoe", cancelOut);
			request.setAttribute("message", e.getMessage());
			return "edit";
		}
	}

	/**
	 * 删除
	 * 
	 * @return
	 */
	public String del() {
		String[] ids = request.getParameterValues("id");
		for (String id : ids) {
			CancelShoe cancelShoe = new CancelShoe();
			// OrderShoe orderShoe = new OrderShoe();
			if (null != id && !("".equals(id))) {
				cancelShoe.setId(Long.parseLong(id));
				cancelOutService.delete(cancelOut);
			}
		}
		return "list";
	}

	public PageController getPager() {
		return pager;
	}

	@Resource
	public void setPager(PageController pager) {
		this.pager = pager;
	}

	public OrderShoeService getOrderShoeService() {
		return orderShoeService;
	}

	@Resource
	public void setOrderShoeService(OrderShoeService orderShoeService) {
		this.orderShoeService = orderShoeService;
	}

	public ShoeService getShoeService() {
		return shoeService;
	}

	@Resource
	public void setShoeService(ShoeService shoeService) {
		this.shoeService = shoeService;
	}

	public ShoeWarehouseService getShoeWarehouseService() {
		return shoeWarehouseService;
	}

	@Resource
	public void setShoeWarehouseService(ShoeWarehouseService shoeWarehouseService) {
		this.shoeWarehouseService = shoeWarehouseService;
	}

	public CancelOut getCancelOut() {
		return cancelOut;
	}

	public void setCancelOut(CancelOut cancelOut) {
		this.cancelOut = cancelOut;
	}

	public CancelOutService getCancelOutService() {
		return cancelOutService;
	}

	@Resource
	public void setCancelOutService(CancelOutService cancelOutService) {
		this.cancelOutService = cancelOutService;
	}

	@Override
	public CancelOut getModel() {
		// TODO Auto-generated method stub
		return cancelOut;
	}

}
